<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 前台查询月度排行ajax类
 * @package  ajax
 * @subpackage  shop
 * @author   create by allen.qiang
 */

quanli_require('module/base/goods/quanliGoods.class.php');

class ajax_monthRanking {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && quanli_is_int($_REQUEST['operFlg'])) {

            switch($_REQUEST['operFlg']) {

                case 1: //top 10
                    $this->searchTop();
                    break;

                default:
                    returnJsonData('参数错误');
                    break;
            }

        } else {
            returnJsonData('参数错误');
        }

    }



    private function searchTop() {

        global $G_SHOP;

        if( !isset($_REQUEST['saleType']) || !quanli_is_int($_REQUEST['saleType'])){
            returnJsonData('参数错误');
        }

        //城市ID
        $city_id = $G_SHOP->get_cookie_info('EBSIG_CITY_ID');
        if (!$city_id) {
            $city_array = $G_SHOP->getCache('city');                                        //读取缓存城市数据
            $city_id = $city_array[0]['id'];
        }

        if( $_REQUEST['saleType'] == 1){
            $search_sql =' SELECT * FROM goods_list WHERE bigCategoryID = 1 AND useFlg = 1 AND cityID = ' . $city_id .' ORDER BY IF(changeSaleType=0,saletotal + changeSaleTotal,saletotal-changeSaleTotal) DESC LIMIT 10 ';
        }else{
            $search_sql = ' SELECT goods_list.* FROM goods_list RIGHT JOIN content_comments ON  goods_list.postID=content_comments.twoPostID WHERE bigCategoryID = 1 AND useFlg = 1 AND cityID = ' . $city_id .' GROUP BY content_comments.twoPostID ORDER BY COUNT(content_comments.twoPostID) DESC LIMIT 10 ';
        }
        $goods_list_array =&$G_SHOP->DBCA->stmt_query($search_sql);
        if (!$goods_list_array) {
            returnJsonData('没有商品数据', 404);
        }

        $goods_array = array();
        foreach($goods_list_array as $key=>&$item) {

            //查询评价的数量
            $comments_sql = 'SELECT COUNT(*) as total FROM content_comments WHERE twoPostID = '.$item->postID;
            $comments_total = &$G_SHOP->DBCA->stmt_query( $comments_sql );
            $comments_total = $comments_total[0]->total;
            //查询的数量
            $goods_sql = 'SELECT IF(changeSaleType=0,saletotal + changeSaleTotal,saletotal-changeSaleTotal) as total FROM goods_list WHERE postID = '.$item->postID;
            $goods_total = &$G_SHOP->DBCA->stmt_query( $goods_sql );
            $goods_total = $goods_total[0]->total;

            $goods_array[] = array(
                'wap_link' => set_goods_link( $item->uid, 2 ),
                'img_link' => set_goods_img_link( $item->goodsMainPic ),
                'frenchName' => $item->frenchName,
                'goodsName' => $item->goodsName,
                'salePrice' => $item->salePrice,
                'postID'=>$item->postID,
                'uid'=>$item->uid,
                'goods_total'=>$goods_total,
                'comments_total'=>$comments_total
            );
        }

        $result_array = array(
            'list' => $goods_array,
        );

        returnJsonData('ok', 200, $result_array);

    }

}